package com.sz.applet.miniuser.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.system.pojo.dto.sysuser.SysUserCreateDTO;
import com.sz.admin.system.pojo.po.SysUser;
import com.sz.admin.teacher.pojo.po.TeacherStatistics;
import com.sz.applet.miniuser.mapper.MiniUserMapper;
import com.sz.applet.miniuser.pojo.dto.MiniLoginDTO;
import com.sz.applet.miniuser.pojo.dto.MiniUserCreateDTO;
import com.sz.applet.miniuser.pojo.po.MiniLoginUser;
import com.sz.applet.miniuser.pojo.po.MiniUser;
import com.sz.applet.miniuser.pojo.vo.MiniUserVO;
import com.sz.applet.miniuser.service.MiniUserService;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.JsonUtils;
import com.sz.core.util.SpringApplicationContextUtils;
import com.sz.core.util.Utils;
import com.sz.platform.enums.AdminResponseEnum;
import com.sz.security.pojo.ClientVO;
import com.sz.security.pojo.LoginInfo;
import com.sz.security.pojo.LoginVO;
import com.sz.security.service.ClientService;
import com.sz.security.service.IAuthStrategy;
import com.sz.wechat.WechatService;
import com.sz.wechat.pojo.LoginInfoResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.sz.applet.miniuser.pojo.po.table.MiniUserTableDef.MINI_USER;

/**
 * <p>
 * 小程序用户表 服务实现类
 * </p>
 *
 * @author sz
 * @since 2024-04-26
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MiniUserServiceImpl extends ServiceImpl<MiniUserMapper, MiniUser> implements MiniUserService {

    private final WechatService wechatService;

    @Override
    public LoginVO doLogin(MiniLoginDTO dto) {
        String clientId = "5037b1ed5acf413587eada3460bafe44";
        ClientService clientService = SpringApplicationContextUtils.getBean(ClientService.class);
        ClientVO client = clientService.getClientByClientId(clientId);

        // 验证code有效性
        CommonResponseEnum.INVALID.message("无效的小程序code").assertFalse(Utils.isNotNull(dto.getCode()));
        // 验证client status 有效性
        CommonResponseEnum.CLIENT_BLOCKED.assertTrue(!("1003001").equals(client.getClientStatusCd()));

        // 调用微信小程序登录策略
        LoginInfo loginInfo =  BeanCopyUtils.copy(dto, LoginInfo.class);

        LoginVO loginvo = IAuthStrategy.login(loginInfo,client,"applet");
        return loginvo;
    }

    @Override
    public MiniLoginUser getUserByOpenId(String openId,String unionid,String phoneNum) {
        // 查询表中是否存在
        QueryWrapper wrapper = QueryWrapper.create()
                .eq(MiniUser::getOpenid, openId);
        MiniUser miniUser = getOne(wrapper);

        if (miniUser == null) {
            MiniUserCreateDTO createdto = new MiniUserCreateDTO();
            createdto.setOpenid(openId);
            createdto.setUnionid(unionid);
            createdto.setNickname(phoneNum);
            createdto.setPhone(phoneNum);
            createdto.setName(phoneNum);
            // todo 创建新的微信用户信息
            MiniUser userinfo = BeanCopyUtils.copy(createdto,MiniUser.class);
            save(userinfo);

            // 查询当前用户
            MiniUser query = getById(userinfo.getId());
            return  BeanCopyUtils.copy(query,MiniLoginUser.class);
        }

        return BeanCopyUtils.copy(miniUser,MiniLoginUser.class);
    }

}